// Fill out your copyright notice in the Description page of Project Settings.

#include "UI/TUPlayerHUDWidget.h"
#include "Component/TUHealthComponent.h"
#include "Component/TUWeaponComponent.h"
#include "TUUtils.h"
#include "Components/ProgressBar.h"
#include "Player/TUPlayerState.h"


void UTUPlayerHUDWidget::NativeOnInitialized()
{
    Super::NativeOnInitialized();

 /*   if (GetOwningPlayer())
    {
        GetOwningPlayer()->GetOnNewPawnNotifier().AddUObject(this, &UTUPlayerHUDWidget::OnNewPawn);
        OnNewPawn(GetOwningPlayerPawn());
    }*/
}
void UTUPlayerHUDWidget::OnNewPawn(APawn* NewPawn)
{
    /*const auto HealthComponent = TUUtils::GetTUPlayerComponent<UTUHealthComponent>(NewPawn);
    if (HealthComponent && !HealthComponent->OnHealthChanged.IsBoundToObject(this))
    {
        HealthComponent->OnHealthChanged.AddUObject(this, &UTUPlayerHUDWidget::OnHealthChanged);
    }*/
}
void UTUPlayerHUDWidget::OnHealthChanged(float Health, float HealthDelta)
{
    /*if (HealthDelta < 0.0f)
    {
        OnTakeDamage();

        if (!IsAnimationPlaying(DamageAnimation))
        {
            PlayAnimation(DamageAnimation);
        }
    }*/
}

float UTUPlayerHUDWidget::GetHealthPercentT() const
{
    const auto HealthComponent = TUUtils::GetTUPlayerComponent<UTUHealthComponent>(GetOwningPlayerPawn());
    if (!HealthComponent) return 0.0f;

    return HealthComponent->GetHealthPercent();
}

bool UTUPlayerHUDWidget::GetCurrentWeaponUIData(FWeaponUIData& UIData) const
{
    const auto WeaponComponent = TUUtils::GetTUPlayerComponent<UTUWeaponComponent>(GetOwningPlayerPawn());
    if (!WeaponComponent) return false;

    return WeaponComponent->GetCurrentWeaponUIData(UIData);
}

bool UTUPlayerHUDWidget::GetCurrentWeaponAmmoData(FAmmoData& AmmoData) const
{
    const auto WeaponComponent = TUUtils::GetTUPlayerComponent<UTUWeaponComponent>(GetOwningPlayerPawn());
    if (!WeaponComponent) return false;

    return WeaponComponent->GetCurrentWeaponAmmoData(AmmoData);
}

bool UTUPlayerHUDWidget::IsPlayerAlive() const
{
    const auto HealthComponent = TUUtils::GetTUPlayerComponent<UTUHealthComponent>(GetOwningPlayerPawn());
    return HealthComponent && !HealthComponent->IsDead();
}

bool UTUPlayerHUDWidget::IsPlayerSpectating() const
{
    const auto Controller = GetOwningPlayer();
    return Controller && Controller->GetStateName() == NAME_Spectating;
}




